/**
 * FileName: ToolbarNav
 * Author: clark
 * Date: 2022/2/4 10:58 下午
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
package com.linkage.linkedlife.ui.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.databinding.DataBindingUtil;

import com.linkage.linkedlife.R;
import com.linkage.linkedlife.databinding.ToolbarNaviBinding;

/**
 * @ClassName: ToolbarNav
 * @Description: java类作用描述
 * @Author: clark
 * @Date: 2022/2/4 10:58 下午
 */
public class ToolbarNav extends Toolbar {
    private static final String TAG = Toolbar.class.getSimpleName();
    private ToolbarNaviBinding toolbarNaviBinding;
    private String titleText;

    public ToolbarNav(@NonNull Context context) {
        super(context);
        init(context, null);

    }

    public ToolbarNav(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ToolbarNav(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }


    private void init(Context context, AttributeSet attrs) {

        View view =LayoutInflater.from(context).inflate(R.layout.toolbar_navi,this);
//
        toolbarNaviBinding=DataBindingUtil.bind(view);
//        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        toolbarNaviBinding = DataBindingUtil.inflate(inflater, R.layout.toolbar_navi, this, false);
//
//        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        DataBindingUtil.inflate(inflater, R.layout.toolbar_navi, this, false);

//        setContentInsetsRelative(0, 0);
//        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//        toolbarNaviBinding = DataBindingUtil.inflate(layoutInflater, R.layout.toolbar_navi, null, false);
//        toolbarNaviBinding.toolbarTitle.setText("ceshishishi");


        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.ToolbarNav, 0, 0);
        titleText = a.getString(R.styleable.ToolbarNav_titleText);
        Log.d(TAG, titleText);
//        toolbarNaviBinding.toolbarTitle.setText(titleText);
//        toolbarNaviBinding.toolbarTitle.setTextColor(getResources().getColor(R.color.colorPrimary));
        a.recycle();

        // Load attributes
    }

    public void setBackEvent(Activity activity) {
        activity.finish();
    }

    public void setTitle(String title) {
        toolbarNaviBinding.toolbarTitle.setText(title);
    }

    public void setTitleByResourceId(int rid) {
        toolbarNaviBinding.toolbarTitle.setText(rid);
    }

    public void setRightButtonImage(int rid) {
        toolbarNaviBinding.toolbarRightImage.setImageResource(rid);
    }

    public void showImage(boolean show) {
        toolbarNaviBinding.toolbarRightImage.setVisibility(show ? View.VISIBLE : View.GONE);
    }


    public void setTitleShow(boolean titleShow) {
        toolbarNaviBinding.toolbarTitle.setVisibility(titleShow ? View.VISIBLE : View.GONE);
    }

    public void hide() {
        this.setVisibility(View.GONE);
    }

    public void setRightButtonOnClickListener(OnClickListener listener) {
        toolbarNaviBinding.toolbarRightImage.setOnClickListener(listener);
    }

    public String getTitleText() {
        return titleText;
    }

}
